//
//  ViewController.m
//  分页
//
//  Created by tanqilong on 16/9/19.
//  Copyright © 2016年 tanqilong. All rights reserved.
//

#import "ViewController.h"
#import "HMSegmentedControl.h"

@interface ViewController () <UIPageViewControllerDataSource,UIPageViewControllerDelegate>
@property (nonatomic,strong) UIPageViewController * pageController;
@property (nonatomic,strong) NSArray * subViewController;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    HMSegmentedControl * segmentedControl = [[HMSegmentedControl alloc]initWithSectionTitles:@[@"one",@"two",@"three"]];
    segmentedControl.frame = CGRectMake(10, 10, 300, 60);
    [segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];
    
    self.pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    _pageController.dataSource = self;
    _pageController.delegate = self;
    
    UIViewController * con1 = [UIViewController new];
    con1.view.backgroundColor = [UIColor yellowColor];
    UIViewController * con2 = [UIViewController new];
    con2.view.backgroundColor = [UIColor blueColor];
    UIViewController * con3 = [UIViewController new];
    con3.view.backgroundColor = [UIColor redColor];
    self.subViewController = @[con1,con2,con3];
    [self addChildViewController:self.pageController];
    [self.view addSubview:self.pageController.view];
    
    self.pageController.view.frame = CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height);
    [self.pageController didMoveToParentViewController:self];
    
    //默认控制器
    [self.pageController setViewControllers:@[con1] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
    self.view.gestureRecognizers = self.pageController.gestureRecognizers;
}

#pragma mark --datasource
//前一个viewcontroller
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [self.subViewController indexOfObject:viewController];
    if (index == 0) {
        return nil;
    }
    return self.subViewController[index -1];
}
//后一个
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self.subViewController indexOfObject:viewController];
    if (index == self.subViewController.count - 1) {
        return nil;
    }
    return self.subViewController[index +1];
}

#pragma mark --delegate
-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    NSLog(@"将要切换");
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    NSLog(@"动画结束");
}

-(void)segmentedControlChangedValue:(HMSegmentedControl *)segment {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
